{include file="public/head"/}
<div class="layui-fluid" id="LAY-app-message">
	<div class="layui-card">
		<div class="layui-card-header">权限列表</div>
		<div class="layui-card-body">
			<div class="clt-auth-btns" style="margin-bottom: 10px;">
				<button class="layui-btn layui-btn-sm" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加权限</button>
			</div>

			<table class="layui-table" id="list" lay-filter="list"></table>
		</div>
	</div>
</div>
{include file="public/foot"/}
<script type="text/html" id="icon">
	<i class="layui-icon {{d.icon}}"></i>
</script>
<script type="text/html" id="auth">
	<input type="checkbox" name="authopen" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="authopen" {{d.authopen == 0 ? 'checked' : ''}}>
</script>
<script type="text/html" id="rule_status">
	<input type="checkbox" name="menustatus" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="menustatus" {{d.menustatus == 1 ? 'checked' : ''}}>
</script>
<script type="text/html" id="rule_action">
	<button class="layui-btn layui-btn-xs" lay-event="auth_edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>
	<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del_btn"><i class="layui-icon layui-icon-delete"></i>删除</button>
</script>
<script type="text/html" id="toolbarAuth">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加权限</button>
	</div>
</script>
<script>
	layui.config({
		base: '__LAYDMIN__/',{if config('app.app_debug')}version: true{/if}
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'admin', 'table', 'util','form','treeGrid'],function(){
		var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form,treeGrid = layui.treeGrid, tableId='list';
		var tableLoadDiv = window.top.layer.load(0,{shade:[0.15, '#000']}); //加载层,table.render完成时需要关闭
		//列表渲染
		treeGrid.render({
			id:tableId
			,elem: '#'+tableId
			,idField:'id'
			,url:'{:url(SYS_PATH."/".CONTROLLER."/index")}'
			,cellMinWidth: 100
			,treeId:'id'//树形id字段名称
			,treeUpId:'pid'//树形父id字段名称
			,treeShowName:'title'//以树形式显示的字段
			,height:'full-140'
			,isFilter:false
			,iconOpen:false//是否显示图标【默认显示】
			,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
			,loading:false
			,method:'post'
			,cols: [[
				{field: 'id', title: '编号', width: 70, fixed: true},
				{field: 'icon', align: 'center', title: '图标', width: 60, templet: '#icon'},
				{field: 'title', title: '权限名称', width: 200},
				{field: 'url', title: '控制器/方法', width: 200,edit:'text'},
				{field: 'name', title: '菜单名', width: 200,edit:'text'},
				{field: 'menustatus', align: 'center', title: '菜单状态', width: 150, toolbar: '#rule_status'},
				{field: 'sort', align: 'center', title: '排序', width: 80, edit:'text'},
				{width: 160, align: 'center', toolbar: '#rule_action'}
			]]
			,page:false,
			done:function (res) {   //返回数据执行回调函数
				window.top.layer.close(tableLoadDiv);    //返回数据关闭loading
			}
		});

		//列表点击编缉
		$('.clt-auth-btns .layui-btn').on('click', function () {
			var type = $(this).data('type');
			switch(type){
				case 'add':
					layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/add')}",'编辑权限');
					break;
			}
		});




		//列表点击编缉
		treeGrid.on('edit(list)', function(obj){
			var value = obj.value //得到修改后的值
					,field = obj.field; //得到字段
			admin.req({
				url: '{:url(SYS_PATH."/".CONTROLLER."/listEdit")}?id='+obj.data.id+'&'+field+'='+value
				, success: function (res) {
					if(res.code==0){
						layer.msg(res.msg,{time:1000,icon:1});
					}else{
						tableAuth.reload();
					}
				}
			});
		});

		//修改菜单状态
		form.on('switch(menustatus)', function(obj){
			loading =layer.load(0, {shade: [0.5,'#fff']});
			var id = this.value;
			var menustatus = obj.elem.checked===true?1:0;
			admin.req({
				url: '{:url(SYS_PATH."/".CONTROLLER."/doStatus")}',
				data: {'id':id,'menustatus':menustatus},
				type:'post',
				success: function (res) {
					if(res.code==0){
						window.top.layer.msg(res.msg,{time:1000,icon:1,shade:[0.5,'#000']});
					}else{
						if(obj.elem.checked==true){
							$(obj.elem).prop('checked',false);
						}else{
							$(obj.elem).prop('checked',true);
						}
						form.render();
					}
					layer.closeAll('loading');
				}
			});
		});

		//监听工具条
		treeGrid.on('tool(list)', function(obj){
			var data = obj.data;
			var id = data.id;
			if(obj.event === 'del_btn'){
				var uri="{:url(SYS_PATH.'/'.CONTROLLER.'/doDelete')}"
				layui.doDelete(uri,"确定要删除吗？",id,obj);//单个删除接口
			} else if(obj.event === 'auth_edit'){
				layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/edit')}?id="+id);
			}
		});



	});
</script>
</body>
</html>